#include "DB.h"
#include "DBPageManager.h"
#include "DBIndexManager.h"
#include "DBError.h"
#include <Windows.h>
#include <process.h>
#include <fcntl.h>
#include <io.h>
#include <sys\types.h> 
#include <sys\stat.h>

/*
   int deleteByTable(char* tableName, int pageid, int offset)
   {
   return 0;
   }

   int deleteByFile(int fileid, int pageid, int offset)
   {
   return 0;
   }
   */

int Delete(whereRet* ret)
{
    whereLoc* location;
    char* tableName = malloc(100);
    char* fileName = malloc(100);
    char* dataBuf;
    int recordSize = 0;
    DB_PAGE* buf;
    int fileid;
    int pageid;
    int error;
    int rowid;
    int colCount;
    int i = 0;
    unsigned long recordCount;
    col* colDef = malloc(sizeof(col));

    while (ret != NULL)
    {
        location = ret->locations;

        strcpy(tableName, location->tableName);
        strcpy(fileName, location->tableName);
        strcat(fileName, ".dbf");
        fileid = location->fileid;
        pageid = location->pageid;
        rowid = location->rowid;

        FILE* fp;
        fp = fopen(fileName, "rb+");
        if (fp == NULL)
        {
            printf("can not find file.\n");
            return -1;
        }

        fread(&recordCount, sizeof(unsigned long), 1, fp);
        fread(&colCount, sizeof(int), 1, fp);


        int vcharNumber = 0;
        for (i = 0; i < colCount; i++)
        {
            fread(colDef, sizeof(col), 1, fp);
            if (colDef->type == DATA_VCHAR)
            {
                vcharNumber = vcharNumber + 1;
            }
            recordSize = recordSize + colDef->size;
        }
        recordSize = recordSize + sizeof(unsigned long) + vcharNumber * sizeof(int) + colCount * sizeof(char);
        fseek(fp, sizeof(unsigned long) + sizeof(int), 0);

        fileid = DBFileIsOpen(tableName);
        if (fileid == -1)
        {
            fileid = DBOpenFile(tableName);
            if (fileid < 0)
            {
                printf("error in DBFileOpen!\n");
                DBPrintError(fileid);
                return -1;
            }
        } 

        if ( (error = DBGetPage(fileid, pageid, &buf)) != DB_OK)
        {
            DBPrintError(error);
            return -1;
        }

        DBPageUsed(fileid, pageid);
        dataBuf =  buf->pgdata;
        dataBuf = dataBuf + recordSize * rowid;

        unsigned long recordNumber = -1;

        memcpy(dataBuf, &recordNumber, sizeof(unsigned long));

        DBPageRelease(fileid, pageid, TRUE);
        DBDisposePage(fileid, pageid);

        recordCount = recordCount - 1;
        fseek(fp, 0, SEEK_SET);
        fwrite(&recordCount, sizeof(unsigned long), 1, fp);
        fclose(fp);
        if (location->nextLoc != NULL)
        {
            //TODO
        }
        
        ret = ret->nextRet;
    }

    free(tableName);
    free(fileName);
    //free(dataBuf);
    //free(buf);
    //free(location);
    free(colDef);

    return 0;
}

